﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;

namespace Labthink.PCSoft.UniversalHost4.Model
{
    /// <summary>
    /// 元数据
    /// 传输中的最小数据单位
    /// </summary>
    public class Metadata : INotifyPropertyChanged, ICloneable
    {
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 数据编号
        /// </summary>
        public int DataNo { get; set; }

        public DataType DataType { get; set; }

        private object _value;

        /// <summary>
        /// 最小值
        /// </summary>
        public double Min { get; set; } = double.MinValue;

        /// <summary>
        /// 最大值
        /// </summary>
        public double Max { get; set; } = double.MaxValue;

        /// <summary>
        /// 字符串最大长度
        /// </summary>
        public double MaxLength { get; set; } = 65535;

        public object OldValue { get; set; }

        public object Value
        {
            get => _value;
            set
            {
                if ((_value ?? "") != (value ?? ""))
                {
                    OldValue = _value;
                    _value = value;
                    OnPropertyChanged(nameof(Value));
                }
            }
        }

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            handler?.Invoke(this, new PropertyChangedEventArgs(name));
        }

        public object Clone()
        {
            return new Metadata
            {
                DataNo = DataNo,
                DataType = DataType,
                Value = Value,
                Min = Min,
                Max = Max,
                MaxLength = MaxLength,
            };
        }
    }

    /// <summary>
    /// 消息数据
    /// </summary>
    [DebuggerDisplay("{DataNo},{Text,nq}")]
    public partial class FullData : Metadata, ICloneable
    {
        /// <summary>
        /// 单位
        /// </summary>
        public string Unit { get; set; }

        /// <summary>
        /// 精度
        /// </summary>
        public int Precision { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// 语言编号
        /// </summary>
        public string LanguageId { get; set; }

        /// <summary>
        /// 列名
        /// </summary>
        public string ColumnName { get; set; }

        public FullData()
        {

        }

        public void FillData(Metadata md)
        {
            Value = md.Value;
        }

        #region ICloneable 成员

        public new object Clone()
        {
            return new FullData
            {
                DataNo = DataNo,
                DataType = DataType,
                Value = Value,
                Unit = Unit,
                Precision = Precision,
                Text = Text,
                LanguageId = LanguageId,
                ColumnName = ColumnName
            };
        }

        #endregion
    }

    public class SimplifyData : ICloneable
    {
        public double Value { get; set; }

        public string Unit { get; set; }

        public object Clone()
        {
            return new SimplifyData
            {
                Value = Value,
                Unit = Unit
            };
        }
    }

    public class DataBar
    {
        public int Id { get; set; }

        /// <summary>
        /// 1 参数，2 数据，3 结果
        /// </summary>
        public int DataType { get; set; }

        /// <summary>
        /// 杯号/腔号，如果只有一个默认为1
        /// </summary>
        public int DataSn { get; set; }


        public int TestType { get; set; }


        public int TestId { get; set; }

        public List<Model.FullData> Datas { get; set; }

        public DataBar()
        {
        }
    }

    public enum DataType
    {
        Int = 1,
        Double = 2,
        String = 3,
        Bool = 4,
        Float = 5,
        Ushort = 6,
        Short = 7,
        Uint = 8,
        Int64 = 9,
        Byte = 10,
        DateTime = 11,
        EDouble = 12
    }
}
